package org.biodas.jdas.dassources.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.biodas.jdas.dassources.Capabilities;
import org.biodas.jdas.dassources.utils.PropInterogator;
import org.biodas.jdas.exceptions.ValidationException;
import org.biodas.jdas.schema.sources.CAPABILITY;
import org.biodas.jdas.schema.sources.PROP;
import org.biodas.jdas.schema.sources.SOURCE;
import org.biodas.jdas.schema.sources.VERSION;
import org.biodas.jdas.schema.types.TYPE;

/**
 * class to wrap a SOURCE object with useful methods for reading the registry
 * generated sources.xml
 * 
 * LPGL
 * 
 * @author JWarren
 * 
 */
public class RegistrySourceAdapter {
	private SOURCE source;
	private Logger logger = Logger.getLogger(RegistrySourceAdapter.class);

	public RegistrySourceAdapter(SOURCE source) {
		this.source = source;
	}

	/**
	 * Get the data source uri e.g. http://das.sanger.ac.uk/das/structure
	 * 
	 * @return
	 * @throws ValidationException
	 */
	public String getOriginalDataSourceUri() throws ValidationException {
		// System.out.println("qUri="+queryUri);
		VERSION version=source.getVERSION().get(0);
		VersionAdapter adapter=new VersionAdapter(version);
		return adapter.getOriginalDataSourceUri();
	
	}

	/**
	 * Get the root uri/server uri e.g. http://das.sanger.ac.uk/das
	 * 
	 * @return
	 * @throws ValidationException
	 */
	public String getServerUri() throws ValidationException {
		String dataSourceUri = this.getOriginalDataSourceUri();
		// System.out.println(dataSourceUri);
		if (!dataSourceUri.contains("/"))
			throw new ValidationException("source with uri=" + source.getUri()
					+ " query_uri doesn't contain a /" + " query_uri="
					+ dataSourceUri);
		return dataSourceUri.substring(0, dataSourceUri.lastIndexOf("/"));
	}
	

	public void setActive() {
		for (VERSION version : source.getVERSION()) {
			List<PROP> props = version.getPROP();
			for (PROP prop : props) {
				if (prop.getName().equals("inactive")) {
					props.remove(prop);// remove the inactive property
				}
				// we have an inactive flag so remove it
			}
		}
	}
	
	public void setInActive() {
		for (VERSION version : source.getVERSION()) {
			List<PROP> props = version.getPROP();
			if(!"".equals(PropInterogator.getPropByName("inactive", props))){;
			//the Version is already inactive so don't do anything
			}else{
			PROP prop=new PROP();
			prop.setName("inactive");
			prop.setValue("0");
			props.add(prop);
			}
		}
	}

	public boolean hasBigFileFormat() {
		
		for(VERSION version:source.getVERSION()){
			for(CAPABILITY cap:version.getCAPABILITY()){
				String type=cap.getType();
				System.out.println(cap);
				if(Capabilities.isBigFileFormat(type)){
					return true;
				}
			}
		}
		return false;
	}
	
public boolean hasCapability(Capabilities capability) {
		
		for(VERSION version:source.getVERSION()){
			for(CAPABILITY cap:version.getCAPABILITY()){
				String type=cap.getType();
				System.out.println(cap.toString());
				if(type.equalsIgnoreCase("das1:"+capability.name())){
					return true;
				}
			}
		}
		return false;
	}


}
